/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import com.beans.CompteRenduActivite;
import com.beans.Projets;
import com.beans.Users;
import com.dao.CompteRenduActivitesDao;
import com.metier.CraMetier;
import com.metier.ProjetMetier;
import com.metier.UsersMetier;

/**
 * 
 * @author Mamadou
 */

/**
 * 
 * @author Mamadou
 */
public class PanelCompteRendu extends javax.swing.JPanel {

	private javax.swing.JButton boutonAjouter;
	private javax.swing.JButton boutonImprimer;
	private javax.swing.JButton boutonModifier;
	private javax.swing.JButton boutonRechercher;
	private javax.swing.JButton boutonSupprimer;
	private javax.swing.JLabel jLabel1, textLabel;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JPanel panelBoutons, panelBouton1;
	private javax.swing.JPanel panelbas;
	private javax.swing.JPanel panelhaut;
	private JTextField textRechercher;
	private JLabel labelDateDebut, labelDateFin;
	private com.toedter.calendar.JDateChooser dateDebutProjet;
	private com.toedter.calendar.JDateChooser dateFinProjet;
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	DefaultTableModel dtm;

	private CompteRenduActivitesDao cradao;
	private JButton boutonAnnuler;
	private long usrid;
	private JTable tablelisteMesCra;

	// End of variables declaration//GEN-END:variables

	public PanelCompteRendu() {

		// initComponents();
	}

	public void initComponents() {

		cradao = new CompteRenduActivitesDao();
		panelhaut = new javax.swing.JPanel();
		jLabel1 = new javax.swing.JLabel();
		panelbas = new javax.swing.JPanel();
		panelbas.setLayout(new GridLayout(1, 1));
		panelBoutons = new javax.swing.JPanel();
		panelBouton1 = new JPanel();
		labelDateDebut = new JLabel("DATE DEBUT");
		labelDateDebut.setBackground(new java.awt.Color(255, 255, 255));
		labelDateFin = new JLabel("DATE FIN");
		labelDateFin.setBackground(new java.awt.Color(255, 255, 255));

		boutonAjouter = new javax.swing.JButton();
		boutonSupprimer = new javax.swing.JButton();
		boutonModifier = new javax.swing.JButton();
		boutonImprimer = new javax.swing.JButton();
		boutonRechercher = new javax.swing.JButton();
		jLabel2 = new javax.swing.JLabel();
		textLabel = new JLabel("");
		textRechercher = new JTextField();
		dateDebutProjet = new com.toedter.calendar.JDateChooser();
		dateFinProjet = new com.toedter.calendar.JDateChooser();

		labelDateDebut.setFont(new java.awt.Font("Verdana", 3, 12));
		labelDateFin.setFont(new java.awt.Font("Verdana", 3, 12));
		labelDateDebut.setForeground(new java.awt.Color(255, 255, 255));
		labelDateFin.setForeground(new java.awt.Color(255, 255, 255));
		boutonAnnuler = new JButton("ANNULER");
		boutonAnnuler.setFont(new java.awt.Font("Verdana", 3, 14));

		setBackground(new java.awt.Color(128, 130, 133));
		setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1,
				new java.awt.Color(102, 153, 255)));
		setFont(new java.awt.Font("Verdana", 3, 14)); // NOI18N
		setMaximumSize(new java.awt.Dimension(1050, 550));
		setMinimumSize(new java.awt.Dimension(1050, 550));
		setLayout(new java.awt.BorderLayout());
		panelBoutons.setLayout(new BorderLayout());
		panelBoutons.setBackground(new java.awt.Color(128, 130, 133));
		panelBouton1.setBackground(new java.awt.Color(128, 130, 133));

		panelhaut.setBackground(new java.awt.Color(128, 130, 133));
		panelhaut.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1,
				1, 1, new java.awt.Color(103, 153, 255)));
		panelhaut.setPreferredSize(new java.awt.Dimension(1050, 75));

		jLabel1.setIcon(new javax.swing.ImageIcon(
				"src/main/java/com/photos/GESTION DES COMPTE RENDU.jpg")); // NOI18N

		panelbas.setBackground(new java.awt.Color(128, 130, 133));
		panelbas.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1,
				1, new java.awt.Color(102, 153, 255)));
		panelbas.setMaximumSize(new java.awt.Dimension(1050, 375));
		panelbas.setMinimumSize(new java.awt.Dimension(1050, 375));
		panelbas.setPreferredSize(new java.awt.Dimension(1056, 410));

		// panelBoutons.setPreferredSize(new java.awt.Dimension(742, 35));
		panelBoutons.setBorder(javax.swing.BorderFactory.createMatteBorder(1,
				1, 1, 1, new java.awt.Color(103, 153, 255)));
		panelBoutons.setMaximumSize(new java.awt.Dimension(1050, 50));
		panelBoutons.setMinimumSize(new java.awt.Dimension(1050, 50));
		panelBoutons.setPreferredSize(new java.awt.Dimension(1050, 40));

		boutonAjouter.setIcon(new javax.swing.ImageIcon(
				"src/main/java/com/photos/add16.png")); // NOI18N
		boutonAjouter.setToolTipText("Ajouter");
		boutonAjouter.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {

				// TODO Auto-generated method stub
				boutonAjouter.setVisible(false);
				boutonModifier.setVisible(false);
				panelbas.removeAll();
				ajouterCompteRendu pan = new ajouterCompteRendu(boutonAnnuler);
				panelbas.add(pan, BorderLayout.SOUTH);
				validate();

			}
		});

		boutonAnnuler.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				panelbas.removeAll();
				jLabel2.setVisible(true);

				boutonModifier.setVisible(true);
				boutonAjouter.setVisible(true);

				panelbas.add(new JScrollPane(tablelisteMesCra),
						BorderLayout.CENTER);
				validate();
			}
		});
		boutonSupprimer.setIcon(new javax.swing.ImageIcon(
				"src/main/java/com/photos/supprimer.gif")); // NOI18N
		boutonSupprimer.setToolTipText("Supprimer");

		boutonModifier.setIcon(new javax.swing.ImageIcon(
				"src/main/java/com/photos/modifier.gif")); // NOI18N
		boutonModifier.setToolTipText("Modifier");

		boutonModifier.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {

				chargeCraModifier();
			}
		});
		boutonImprimer.setIcon(new javax.swing.ImageIcon(
				"src/main/java/com/photos/imprimer.gif")); // NOI18N
		boutonImprimer.setToolTipText("Imprimer");

		boutonRechercher.setIcon(new javax.swing.ImageIcon(
				"src/main/java/com/photos/recherchermod.png")); // NOI18N
		boutonRechercher.setToolTipText("Rechercher");

		jLabel2.setFont(new java.awt.Font("Verdana", 3, 14)); // NOI18N

		panelhaut.add(jLabel1);

		panelBouton1.add(boutonAjouter);
		// panelBouton1.add(boutonSupprimer);
		panelBouton1.add(boutonModifier);

		panelBoutons.add(panelBouton1, BorderLayout.WEST);

		add(panelBoutons, java.awt.BorderLayout.CENTER);
		add(panelhaut, BorderLayout.NORTH);
		tableauListMesCra();
		panelbas.add(new JScrollPane(tablelisteMesCra), BorderLayout.CENTER);
		add(panelbas, BorderLayout.SOUTH);
	}

	// méthode qui permet de charger les cra dans un JTABLE

	public void tableauListMesCra() {

		String[] entetes = { "DATE DEBUT", "DATE FIN", "DUREE PREVUE",
				"DUREE EFFECTIVE", "ACTIVITES", "STATUT" };

		// Long val= this. getUsrid();
		System.out.println("val recupéré  " + getUsrid());
		CraMetier crametier = new CraMetier();
		List<CompteRenduActivite> mescra = crametier
				.mesCompteRenduActivites(this.getUsrid());
		int nombreLigne = mescra.size();
		System.out.println("taille de la liste mes cra" + mescra.size()
				+ this.getUsrid());
		Object data[][] = new Object[nombreLigne][6];

		// On parcoure la liste pour recuperer les cra et remplir le tableau
		// d'objet
		for (int i = 0; i < nombreLigne; i++) {
			CompteRenduActivite cra = new CompteRenduActivite();
			cra = mescra.get(i);

			data[i][0] = cra.getCraDateDeb();
			data[i][1] = cra.getCraDateFin();

			data[i][2] = cra.getActDurePrev();
			data[i][3] = cra.getActDureEff();

			data[i][4] = cra.getActProjet();
			data[i][5] = cra.getActStatut();

		}

		dtm = new DefaultTableModel(data, entetes);

		tablelisteMesCra = new JTable(dtm);

	}

	// méthode pour le chargement des données de l'utilisateur selectionné dans
	// la formulaire

	public void chargeCraModifier() {
		int ligneSelectionner = getTablelisteMesCra().getSelectedRow();
		System.out.println("voici la ligne selectionné " + ligneSelectionner);
		if (ligneSelectionner == -1) {
			JOptionPane
					.showMessageDialog(null,
							"veuillez selectionner un compte rendu d'activité  d'abord!!!!");

		} else {
			CraMetier craMetiers = new CraMetier();
			CompteRenduActivite craModif = new CompteRenduActivite();

			Date datedebutRecuperer = (Date) getTablelisteMesCra().getValueAt(
					ligneSelectionner, 0);
			Date dateFinRecuperer = (Date) getTablelisteMesCra().getValueAt(
					ligneSelectionner, 1);
			String dureprevRecuperer = (String) getTablelisteMesCra()
					.getValueAt(ligneSelectionner, 2);
			String dureEffRecuperer = (String) getTablelisteMesCra()
					.getValueAt(ligneSelectionner, 3);
			String activiteRecuperer = (String) getTablelisteMesCra()
					.getValueAt(ligneSelectionner, 4);
			String statutProjetRecuperer = (String) getTablelisteMesCra()
					.getValueAt(ligneSelectionner, 5);

			// on charge les champs dans le panel PanelModifiercompterendu

			
			  PanelModifierCompteRenduActivite pan = new
			  PanelModifierCompteRenduActivite(); CraMetier crametiers = new
			  CraMetier(); CompteRenduActivite cra = new CompteRenduActivite();
			  
			/* cra =
			  crametiers.findCompteRenduActiviteByDate(datedebutRecuperer);*/
			 
			  pan.getTextActivite().setText(activiteRecuperer);
			//  pan.getTextCommentaire().setText(cra.getCraDescription());
			  pan.getTextDuresEffect().setText(dureEffRecuperer);
			  pan.getTextDuresPrevus().setText(dureprevRecuperer);
			  pan.getDatedebut().setDate(datedebutRecuperer);
			  pan.getDatefin().setDate(dateFinRecuperer);
			  
			 panelbas.removeAll(); boutonAjouter.setVisible(false);
			  
			 boutonModifier.setVisible(false);
			
			// pan.setReferencecraDateCreate(datedebutRecuperer);
			
			
			  panelbas.add(pan,BorderLayout.SOUTH); validate();
			 validate();

		}
	}

	public JPanel getFrame() {
		return this;
	}

	public JTextField getTextRechercher() {
		return textRechercher;
	}

	public void setTextRechercher(JTextField textRechercher) {
		this.textRechercher = textRechercher;
	}

	/**
	 * @return the usrid
	 */
	public long getUsrid() {
		return usrid;
	}

	/**
	 * @param usrid
	 *            the usrid to set
	 */
	public void setUsrid(long usrid) {
		this.usrid = usrid;
	}

	/**
	 * @return the panelbas
	 */
	public javax.swing.JPanel getPanelbas() {
		return panelbas;
	}

	/**
	 * @param panelbas
	 *            the panelbas to set
	 */
	public void setPanelbas(javax.swing.JPanel panelbas) {
		this.panelbas = panelbas;
	}

	/**
	 * @return the tablelisteMesCra
	 */
	public JTable getTablelisteMesCra() {
		return tablelisteMesCra;
	}

	/**
	 * @param tablelisteMesCra
	 *            the tablelisteMesCra to set
	 */
	public void setTablelisteMesCra(JTable tablelisteMesCra) {
		this.tablelisteMesCra = tablelisteMesCra;
	}

}
